package com.ys.service.sys.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ys.entity.sys.*;
import com.ys.mapper.sys.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.config.MyConfig;
import com.ys.dto.OptionDto;
import com.ys.entity.flow.YspDef;
import com.ys.entity.flow.YspDefLink;
import com.ys.entity.flow.YspDefNode;
import com.ys.entity.flow.YspDeploy;
import com.ys.entity.flow.YspModel;
import com.ys.enume.sys.YsSynLogEnum;
import com.ys.mapper.flow.YspDefLinkMapper;
import com.ys.mapper.flow.YspDefMapper;
import com.ys.mapper.flow.YspDefNodeMapper;
import com.ys.mapper.flow.YspDeployMapper;
import com.ys.mapper.flow.YspModelMapper;
import com.ys.service.sys.YsSynService;
import com.ys.util.TxtUtil;

@Service("ysSynService")
public class YsSynServiceImpl implements YsSynService {

	@Autowired
	private SysMapper sysMapper;

	@Autowired
	private YsButtonMapper ysButtonMapper;
	@Autowired
	private YsColMapper ysColMapper;
	@Autowired
	private YsDictMapper ysDictMapper;
	@Autowired
	private YsGridMapper ysGridMapper;
	@Autowired
	private YsGridButtonMapper ysGridButtonMapper;
	@Autowired
	private YsGridColMapper ysGridColMapper;
	@Autowired
	private YsMenuMapper ysMenuMapper;
	@Autowired
	private YsModalSelectorMapper ysModalSelectorMapper;
	@Autowired
	private YsModalSelectorFieldMapper ysModalSelectorFieldMapper;
	@Autowired
	private YspDefMapper yspDefMapper;
	@Autowired
	private YspDefLinkMapper yspDefLinkMapper;
	@Autowired
	private YspDefNodeMapper yspDefNodeMapper;
	@Autowired
	private YspDeployMapper yspDeployMapper;
	@Autowired
	private YspModelMapper yspModelMapper;
	@Autowired
	private YsRowClickGridColMapper ysRowClickGridColMapper;

	@Override
	public void syn(Integer type, String body) {
		JSONArray bodyJA = JSON.parseArray(body);
		
		String tableName = YsSynLogEnum.Type.getTableName(type);
		sysMapper.disableIDENTITY_INSERT(tableName);
		for (int i = 0; i < bodyJA.size(); i++) {
			JSONObject bodyJO = bodyJA.getJSONObject(i);
			
			if (type == YsSynLogEnum.Type.YS_Menu.value) {
				YsMenu entity = JSON.parseObject(bodyJO.toJSONString(), YsMenu.class);
				if(new YsMenu().setId(entity.getId()).count() > 0) {
					ysMenuMapper.update(entity);
				} else {
					ysMenuMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YS_Col.value) {
				YsCol entity = JSON.parseObject(bodyJO.toJSONString(), YsCol.class);
				if(new YsCol().setId(entity.getId()).count() > 0) {
					ysColMapper.update(entity);
				} else {
					ysColMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YS_Button.value) {
				YsButton entity = JSON.parseObject(bodyJO.toJSONString(), YsButton.class);
				if(new YsButton().setId(entity.getId()).count() > 0) {
					ysButtonMapper.update(entity);
				} else {
					ysButtonMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YS_ModalSelector.value) {
				YsModalSelector entity = JSON.parseObject(bodyJO.toJSONString(), YsModalSelector.class);
				if(new YsModalSelector().setId(entity.getId()).count() > 0) {
					ysModalSelectorMapper.update(entity);
				} else {
					ysModalSelectorMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YS_ModalSelectorField.value) {
				YsModalSelectorField entity = JSON.parseObject(bodyJO.toJSONString(), YsModalSelectorField.class);
				if(new YsModalSelectorField().setId(entity.getId()).count() > 0) {
					ysModalSelectorFieldMapper.update(entity);
				} else {
					ysModalSelectorFieldMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YS_Grid.value) {
				YsGrid entity = JSON.parseObject(bodyJO.toJSONString(), YsGrid.class);
				if(new YsGrid().setId(entity.getId()).count() > 0) {
					ysGridMapper.update(entity);
				} else {
					ysGridMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YS_GridCol.value) {
				YsGridCol entity = JSON.parseObject(bodyJO.toJSONString(), YsGridCol.class);
				if(new YsGridCol().setId(entity.getId()).count() > 0) {
					ysGridColMapper.update(entity);
				} else {
					ysGridColMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YS_GridButton.value) {
				YsGridButton entity = JSON.parseObject(bodyJO.toJSONString(), YsGridButton.class);
				if(new YsGridButton().setId(entity.getId()).count() > 0) {
					ysGridButtonMapper.update(entity);
				} else {
					ysGridButtonMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YSP_Def.value) {
				YspDef entity = JSON.parseObject(bodyJO.toJSONString(), YspDef.class);
				if(new YspDef().setId(entity.getId()).count() > 0) {
					yspDefMapper.update(entity);
				} else {
					yspDefMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YSP_DefLink.value) {
				YspDefLink entity = JSON.parseObject(bodyJO.toJSONString(), YspDefLink.class);
				if(new YspDefLink().setId(entity.getId()).count() > 0) {
					yspDefLinkMapper.update(entity);
				} else {
					yspDefLinkMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YSP_DefNode.value) {
				YspDefNode entity = JSON.parseObject(bodyJO.toJSONString(), YspDefNode.class);
				if(new YspDefNode().setId(entity.getId()).count() > 0) {
					yspDefNodeMapper.update(entity);
				} else {
					yspDefNodeMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YSP_Deploy.value) {
				YspDeploy entity = JSON.parseObject(bodyJO.toJSONString(), YspDeploy.class);
				if(new YspDeploy().setId(entity.getId()).count() > 0) {
					yspDeployMapper.update(entity);
				} else {
					yspDeployMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YSP_Model.value) {
				YspModel entity = JSON.parseObject(bodyJO.toJSONString(), YspModel.class);
				if(new YspModel().setId(entity.getId()).count() > 0) {
					yspModelMapper.update(entity);
				} else {
					yspModelMapper.insert(entity);
				}
			} else if (type == YsSynLogEnum.Type.YS_Dict.value) {
				
				YsDict entity = JSON.parseObject(bodyJO.toJSONString(), YsDict.class);
				if(new YsDict().setId(entity.getId()).count() > 0) {
					ysDictMapper.update(entity);
				} else {
					ysDictMapper.insert(entity);
				}

				// 生成静态JSON文件，便于前端快速访问
				String filePath = MyConfig.getProfile() + "/sys/";
				String fileName = "dict.json";
				
				Map<String, List<OptionDto>> MAP_KEY_OPTIONS = new HashMap<String, List<OptionDto>>();
				// 加载字典
				List<YsDict> listYsDict = new YsDict().setStatus(1).setOrderby("A.DictKey asc, A.SortNo asc").queryList();
				for(YsDict ysDict : listYsDict) {
					String dictKey = ysDict.getDictKey();
					List<OptionDto> listOption = MAP_KEY_OPTIONS.containsKey(dictKey) ? MAP_KEY_OPTIONS.get(dictKey) : new ArrayList<OptionDto>();
					
					String name = ysDict.getDictName();
					String valueStr = ysDict.getDictValue();
					Object value = valueStr.matches("^[+-]?\\d+$") ? Integer.parseInt(valueStr) : valueStr;
					listOption.add(new OptionDto(name, value));
					MAP_KEY_OPTIONS.put(dictKey, listOption);
				}
				String content = "var MAP_DICT = " + JSON.toJSONString(MAP_KEY_OPTIONS) + "\n";
				
				// 加载弹出框字段
				
				// 弹出框字段
				Map<String, ModalConfigDto> MAP_KEY_MODAL = new HashMap<String, ModalConfigDto>();
				
				List<YsModalSelector> listYsModalSelector = new YsModalSelector().queryList();
				for(YsModalSelector modal : listYsModalSelector) {
					Integer multySelected = null;
					String pathGridRequest = modal.getPathGridRequest();
					
					List<ModalConfigDto.ColGridDto> colGrids = new ArrayList<ModalConfigDto.ColGridDto>();
					
					// 搜索字段
					List<ModalConfigDto.ConditionDto> conditions = new ArrayList<ModalConfigDto.ConditionDto>();
					List<YsModalSelectorField> listYsModalSelectorField = new YsModalSelectorField().setPid(modal.getId()).setOrderby("A.SortNo ASC").queryList();
					for(YsModalSelectorField f : listYsModalSelectorField) {
						{
							String title = f.getName();
							String code = f.getCode();
							String type2 = f.getType();
							Integer width = f.getWidth();
							Integer align = f.getAlign();
							String dictKeyBase = f.getDictKeyBase();
							colGrids.add(new ModalConfigDto.ColGridDto(title, code, type2, width, align, dictKeyBase));
						}
						
						if(f.getStateCondition() == 1){
							String code = f.getCode();
							String type2 = f.getType();
							String rangeType = "t_bothlike";
							String name = f.getName();
							String value = null;
							String placeHolder = null;
							String dictKeyBase = f.getDictKeyBase();
							conditions.add(new ModalConfigDto.ConditionDto(code, type2, rangeType, name, value, placeHolder, dictKeyBase));
						}
					}
					
					ModalConfigDto dto = new ModalConfigDto(multySelected, pathGridRequest, colGrids, conditions);
					MAP_KEY_MODAL.put(modal.getNameTable(), dto);
				}
				content += "var MAP_MODAL = " + JSON.toJSONString(MAP_KEY_MODAL) + "\n";
				
				TxtUtil.writerTxtFile(filePath, fileName, content);
			} else if (type == YsSynLogEnum.Type.YS_RowClickGridCol.value) {
				YsRowClickGridCol entity = JSON.parseObject(bodyJO.toJSONString(), YsRowClickGridCol.class);
				if(new YsGridCol().setId(entity.getId()).count() > 0) {
					ysRowClickGridColMapper.update(entity);
				} else {
					ysRowClickGridColMapper.insert(entity);
				}
		}
		}
		sysMapper.enableIDENTITY_INSERT(tableName);
	}
}
